<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="发车日期" prop="startDate">
        <el-date-picker
          v-model="queryParams.startDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="-" prop="endDate" label-width="10">
        <el-date-picker
          v-model="queryParams.endDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="预定日期" prop="preStartDate">
        <el-date-picker
          v-model="queryParams.preStartDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="-" prop="preEndDate" label-width="10">
        <el-date-picker
          v-model="queryParams.preEndDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款状态" prop="refundStatus">
        <el-select
          v-model="queryParams.refundStatus"
          placeholder="订单状态"
          clearable
          size="small"
          style="width: 150px"
        >
          <el-option
            v-for="dict in statusList"
            :key="dict.status"
            :label="dict.value"
            :value="dict.status"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        label="申请退款日期"
        prop="operateStartDate"
        label-width="50"
      >
        <el-date-picker
          v-model="queryParams.operateStartDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="-" prop="operateEndDate" label-width="10">
        <el-date-picker
          v-model="queryParams.operateEndDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分销商" prop="traderAccount">
        <el-select
          v-model="queryParams.traderAccount"
          placeholder="选择分销商"
          clearable
          size="small"
          style="width: 150px"
        >
          <el-option
            v-for="dict in distributor"
            :key="dict.trader"
            :label="dict.value"
            :value="dict.trader"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出发地" prop="stationCode">
        <el-autocomplete
          class="inline-input"
          clearable
          v-model="queryParams.stationName"
          :fetch-suggestions="StationQuerySearch"
          placeholder="请输入内容"
          :trigger-on-focus="false"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="目的地" prop="targetName">
        <el-input
          v-model="queryParams.targetName"
          placeholder="请输入目的地"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="搜索" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入订单号、手机号、班次搜索"
          clearable
          style="width: 280px"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          v-hasPermi="['order:refundList:query']"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:refundList:export']"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="postList"
      :max-height="tableHeight"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="订单编号" align="center" prop="orderCode" />
      <el-table-column label="渠道" align="center" prop="trader" />
      <el-table-column label="退款状态" align="center" prop="status">
        <template slot-scope="scope">
          <p v-if="postList[scope.$index].status == 0">处理中</p>
          <p v-if="postList[scope.$index].status == 1">已退款</p>
          <p v-if="postList[scope.$index].status == 2">已撤销</p>
          <p v-if="postList[scope.$index].status == 3">第三方已处理</p>
        </template>
      </el-table-column>
      <el-table-column label="出发地" align="center" prop="stationName">
      </el-table-column>
      <el-table-column label="目的地" align="center" prop="targetName">
      </el-table-column>
      <el-table-column
        label="预定时间"
        align="center"
        prop="preTime"
        width="150px"
      >
        <template slot-scope="scope">
          <span>{{ newDate(scope.row.preTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发车时间" align="center" prop="startDate" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="用户手机号" align="center" prop="userPhone" />
      <el-table-column label="支付金额" align="center" prop="totalPay" />
      <el-table-column label="退款金额" align="center" prop="refundMoney" />
      <el-table-column label="扣除手续费" align="center" prop="fee" />
      <el-table-column label="退款原因" align="center" prop="refundReason" />
      <el-table-column label="申请人" align="center" prop="applyPerson" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="applyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ newDate(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="orderMessage(scope.row)"
            v-hasPermi="['system:post:edit']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList()"
    />
  </div>
</template>

<script>
import {
  listPost,
  getPost,
  delPost,
  addPost,
  updatePost,
} from "@/api/system/post";
import {
  queryrefund,
  querystations,
  querytargets,
  querytrader,
  downloadrefund,
} from "@/api/order/refundList";
import utils from "../../js/index";
export default {
  name: "RefundList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 状态列表
      statusList: [
        {
          status: 100,
          value: "全部",
        },
        {
          status: 0,
          value: "处理中",
        },
        {
          status: 1,
          value: "已退款",
        },
        {
          status: 2,
          value: "已撤销",
        },
        {
          status: 3,
          value: "第三方已处理",
        },
      ],
      // 分销商列表
      distributor: [],
      // 目的地列表
      //   departList: [],
      // 出发地列表
      stationList: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        preStartDate: "", //预定日期
        preEndDate: "",
        startDate: undefined, //发车日期
        endDate: undefined,
        operateStartDate: undefined, // 申请退款日期
        operateEndDate: undefined,
        refundStatus: undefined, //退款状态
        traderAccount: undefined, //分销商
        stationCode: undefined, //出发地
        targetName: undefined, //目的地
        keyword: undefined, //关键词
        stationName: undefined,
      },
      globalCode: "",
      //预定日期
      reserve: [],
      // 发车日期
      depart: [],
      //申请退款日期
      refundDate: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" },
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" },
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" },
        ],
      },
      // 取消退款模态框
      dialogVisible: false,
      // 取消退款参数
      refModel: {
        orderCode: undefined,
        cancleReason: undefined,
      },
    };
  },
  created() {
    // 当前屏幕高度
    this.tableHeight = window.innerHeight - 350;
    this.getList(); //退款列表查询
    this.getQuerytrader(); //分销商
    this.getStations(); // 出发地
    this.getDicts("sys_normal_disable").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    // 车站模糊查询
    StationQuerySearch(queryString, cb) {
      var restaurantsStation = this.stationList;
      var results = queryString
        ? restaurantsStation.filter(this.StationCreateFilter(queryString))
        : restaurantsStation;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    StationCreateFilter(queryString) {
      return (v) => {
        return v.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
      };
    },
    //   预定日期
    getpreStartDate(val) {
      if (!val) {
        this.queryParams.preStartDate = "";
        this.queryParams.preEndDate = "";
        return;
      }
      this.queryParams.preStartDate = this.newDate(val[0]);
      this.queryParams.preEndDate = this.newDate(val[1]);
    },
    // 发车日期
    getDepart(val) {
      if (!val) {
        this.queryParams.startDate = "";
        this.queryParams.endDate = "";
        return;
      }
      this.queryParams.startDate = this.newDate(val[0]);
      this.queryParams.endDate = this.newDate(val[1]);
    },
    // 申请退款日期
    getRefund(val) {
      if (!val) {
        this.queryParams.operateStartDate = "";
        this.queryParams.operateEndDate = "";
      }
      this.queryParams.operateStartDate = this.newDate(val[0]);
      this.queryParams.operateEndDate = this.newDate(val[1]);
    },
    cancleRef(code) {
      this.dialogVisible = true;
      this.refModel.orderCode = code.orderCode;
    },
    orderMessage(order) {
      // 跳转到详情页面
      this.$router.push({
        path: `./orderMessage`,
        query: {
          orderCode: order.orderCode,
        },
      });
    },
    newDate(date) {
      return utils.utils.NewDate(date);
    },
    // 分销商列表
    getQuerytrader() {
      querytrader().then((response) => {
        this.distributor = response.data;
      });
    },
    /** 查询退款订单列表 */
    getList() {
      queryrefund(this.queryParams).then((response) => {
        if (response.code == 200) {
          // console.log(response);
          this.postList = response.data.refundInfos;
          this.total = response.data.totalPage;
          this.loading = false;
        }
      });
    },
    // 获取所有出发站
    getStations() {
      querystations().then((res) => {
        // console.log(res);
        if (res.code == 200) {
          var arr = [];
          res.data.forEach((item) => {
            arr.push({ value: item.stationName });
          });
          this.stationList = arr;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 导出按钮操作 */
    handleExport() {
      downloadrefund(this.queryParams);
    },
  },
};
</script>
